﻿using System;
using System.Collections.Generic;
using System.ServiceModel;

namespace SharedGenomics.Core
{
    public class WCFClientHost
    {
        protected virtual void ExecuteProxy<TContract>(Action<ClientBase<TContract>> proxyAction) where TContract : class
        {
            using (var proxy = this.ProxyFactory.GetProxy<TContract>())
            {
                proxyAction(proxy);
            }
        }

        protected virtual TOut ExecuteProxy<TContract, TOut>(Func<ClientBase<TContract>, TOut> proxyFunc) where TContract : class
        {
            using (var proxy = this.ProxyFactory.GetProxy<TContract>())
            {
                return proxyFunc(proxy);
            }
        }

        protected virtual void ExecuteProxyContract<TContract>(Action<TContract> proxyAction) where TContract : class
        {
            using (var proxy = this.ProxyFactory.GetProxy<TContract>())
            {
                TContract channel = proxy.ChannelFactory.CreateChannel();
                proxyAction(channel);
            }
        }

        protected virtual TOut ExecuteProxyContract<TContract, TOut>(Func<TContract, TOut> proxyFunc) where TContract : class
        {
            using (var proxy = this.ProxyFactory.GetProxy<TContract>())
            {
                TContract channel = proxy.ChannelFactory.CreateChannel();
                return proxyFunc(channel);
            }
        }

        [Microsoft.Practices.Unity.Dependency]
        public WCFProxyFactory ProxyFactory { protected get; set; }
    }
}
